FF = {}
FN = {}

function Ls()
    print("There is(are) " .. #FF .. " item(s).")
    for i, v in ipairs(FF) do
        print("#" .. i .. ":The value of " .. v .. " is " .. FN[i] .. ".")
    end
end

function AddItem(name, value)
    local pos = 0
    for i, v in ipairs(FF) do
        if v == name then
            pos = i
            break
        end
    end
    if pos ~= 0 then
        print("The item " .. name .. " already exists. To update the value, please remove the item first.")
        return
    end
    table.insert(FF, name)
    table.insert(FN, value)
    if type(value) == "number" then
        print("Done.")
    else
        print("ERROR: Received a non-number as a value, please check your input.")
    end
end

function Rm(name)
    local pos = 0
    for i, v in ipairs(FF) do
        if v == name then
            pos = i
            break
        end
    end
    if pos == 0 then
        print("ERROR: Not finding match file, please check your input.")
    else
        table.remove(FF, pos)
        table.remove(FN, pos)
        print("Done.");
    end
end

function Query(name)
    local pos = 0
    for i, v in ipairs(FF) do
        if v == name then
            pos = i
            break
        end
    end
    if pos == 0 then
        print("ERROR: Not finding match file, please check your input.")
    else
        print("The value of " .. name .. " is " .. FN[pos] .. ".")
    end
end

do
    while true do
        os.execute("cls")
        print("##########  TEST2 STORAGE SYSTEM  ##########")
        print("1 | Add an item.")
        print("2 | Query an item.")
        print("3 | List all the items.")
        print("4 | Remove an item.")
        print("5 | Exit.")
        print("Please input a command number:")
        local cmd = tonumber(io.read())
        if cmd == 5 then
            break
        elseif cmd == 1 then
            print("Please input the name of the item.")
            local a = io.read()
            print("Please input the value of the item.")
            local b = tonumber(io.read())
            AddItem(a, b)
            print("Press [ENTER] to continue.")
            io.read()
        elseif cmd == 2 then
            print("Please input the item you would like to query.")
            Query(io.read());
            print("Press [ENTER] to continue.")
            io.read()
        elseif cmd == 3 then
            Ls()
            print("Press [ENTER] to continue.")
            io.read()
        elseif cmd == 4 then
            print("Please input the item you would like to remove.")
            Rm(io.read())
            print("Press [ENTER] to continue.")
            io.read()
        else
            print("No such command. Press [ENTER] to continue.")
            io.read()
        end
    end
end

